package com.group.service.impl;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbCustomerMapper;
import com.group.pojo.Common;
import com.group.pojo.TbCustomer;
import com.group.pojo.TbCustomerExample;
import com.group.pojo.TbCustomerExample.Criteria;
import com.group.service.CommonService;
import com.group.service.CustomerService;
import com.group.util.CommonUtil;
import com.group.util.MD5;

@Service
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private TbCustomerMapper customerMapper;
	@Autowired
	private CommonService commonService;
	
	
	private void setCondition(Criteria criteria, TbCustomer customer) {
		String id = customer.getId();
		if (id != null && !"".equals(id)) {
			criteria.andIdEqualTo(id);
		}
		
		String customerStatus = customer.getCustomerStatus();
		if (customerStatus!= null && !"".equals(customerStatus)) {
			criteria.andCustomerStatusEqualTo(customerStatus);
		}
		
		String loginName = customer.getLoginName();
		if (loginName != null && !"".equals(loginName)) {
			criteria.andLoginNameEqualTo(loginName);
		}
		
		String customerName = customer.getCustomerName();
		if (customerName!= null && !"".equals(customerName)) {
			criteria.andCustomerNameEqualTo(customerName);
		}
	}
	
	@Override
	public List<TbCustomer> query(TbCustomer customer) {
		TbCustomerExample example = new TbCustomerExample();
		
		setCondition(example.createCriteria(), customer);
		
		List<TbCustomer> list = customerMapper.selectByExample(example);
		return list;
	}

	@Override
	public Map<String, String> login(TbCustomer customer, HttpServletRequest request) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		
		String customerName = customer.getCustomerName();
		
		TbCustomer cus = new TbCustomer();
		cus.setCustomerName(customerName);
		List<TbCustomer> query = query(cus);
		
		if (query != null && query.size() == 1) {
			cus = query.get(0);
			String customerPassword = cus.getCustomerPassword();
			String comfirm = customer.getCustomerPassword();
			if (MD5.encrypt(comfirm).equals(customerPassword)) {
				//session
				request.getSession().setAttribute("CUSTOMER_SESSION", cus);
				
				returnMap.put("success", "true");
			}
		} else {
			cus.setCustomerName(null);
			cus.setLoginName(customerName);
			query = query (cus);
			if (query != null && query.size() == 1) {
				cus = query.get(0);
				String customerPassword = cus.getCustomerPassword();
				String comfirm = customer.getCustomerPassword();
				if (MD5.encrypt(comfirm).equals(customerPassword)) {
					//session
					request.getSession().setAttribute("CUSTOMER_SESSION", cus);
					
					returnMap.put("success", "true");
				}
			}
		}
		return returnMap;
	}

	@Override
	public Map<String, String> isLogin(HttpServletRequest request) {
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "false");
		Object customers = request.getSession().getAttribute("CUSTOMER_SESSION");
		if (null != customers) {
			TbCustomer customer = (TbCustomer) customers;
			if (null != customer.getId()) {
				returnMap.put("success", "true");
				returnMap.put("customerName", customer.getCustomerName());
			}
		}
		return returnMap;
	}

	@Override
	public Map<String, String> logout(HttpServletRequest request) {
		request.getSession().removeAttribute("CUSTOMER_SESSION");
		Map<String, String> returnMap = CommonUtil.getReturnMap("success", "true");
		return returnMap;
	}



}
